package com.wanxi.repertory.service.service;

import com.wanxi.repertory.service.api.dto.EnterpriseRepertoryDto;
import com.wanxi.repertory.service.api.dto.ShopRepertoryDto;
import com.wanxi.repertory.service.api.feign.RepertoryShopService;
import com.wanxi.repertory.service.api.query.RepertoryQuery;
import com.wanxi.repertory.service.api.result.CommonResult;
import com.wanxi.repertory.service.api.util.RepertoryChangeEnum;
import com.wanxi.repertory.service.api.vo.*;
import com.wanxi.repertory.service.core_service.RepertoryTotalService;
import com.wanxi.repertory.service.utill.Check;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "库存接口")
@RequestMapping("/repertory")
@RestController
public class RepertoryShopServiceImpl implements RepertoryShopService {
    @Resource
    RepertoryTotalService repertoryTotalService;

    @Resource
    Check check;

    @Override
    @ApiOperation("根据店铺id查找店铺下所有sku")
    @ApiImplicitParam(name = "shopId", value = "查找的店铺id", dataType = "Long")
    @GetMapping("/sku/findSkuIdByShopId")
    public CommonResult<List<Long>> findSkuIdByShopId(Long shopId) {
        return CommonResult.success(repertoryTotalService.findSkuIdByShopId(shopId));
    }

    @Override
    @ApiOperation("查找商品id根据销售量排序")
    @GetMapping("/sku/findSkuIdDescSaleTotal")
    public CommonResult<List<Long>> findSkuIdDescSaleTotal() {
        return null;
    }

    @Override
    @ApiOperation("通过sku id查找库存量与销售量")
    @ApiImplicitParam(name = "skuId", value = "要查看库存的sku id", dataType = "Long")
    @GetMapping("/total/findBySkuId")
    public CommonResult<RepertoryTotalFindByIdOutVo> findTotalBySkuId(Long skuId) {
        /*通过sku id查找库存量与销售量*/
        ShopRepertoryDto shopRepertoryDto = repertoryTotalService.findShopRepertoryBySkuId(skuId);
        RepertoryTotalFindByIdOutVo repertoryFindByIdOutVo = RepertoryTotalFindByIdOutVo.builder()
                .saleTotal(shopRepertoryDto.getSaleTotal())
                .remainTotal(shopRepertoryDto.getRemainTotal())
                .build();
        return CommonResult.success(repertoryFindByIdOutVo);
    }

    @Override
    @ApiOperation("修改库存量及商品上下架")
    @PutMapping("/total/update")
    public CommonResult updateTotal(RepertoryTotalUpdateInVo repertoryUpdateInVo) {
        if (check.checkShopSku(repertoryUpdateInVo.getShopId(), repertoryUpdateInVo.getSkuId())) {
            return CommonResult.failed("该店铺无法管理该商品");
        }
        try {
            ShopRepertoryDto shopRepertoryDto = repertoryTotalService.findShopRepertoryBySkuId(repertoryUpdateInVo.getSkuId());
            /*修改库存量*/
            if (repertoryUpdateInVo.getChangeTotal() != 0) {
                if (repertoryUpdateInVo.getChangeType() == RepertoryChangeEnum.库存增加.getCode()) {
                    shopRepertoryDto.setRemainTotal(shopRepertoryDto.getRemainTotal() + repertoryUpdateInVo.getChangeTotal());
                } else if (repertoryUpdateInVo.getChangeType() == RepertoryChangeEnum.库存减少.getCode()) {
                    shopRepertoryDto.setRemainTotal(shopRepertoryDto.getRemainTotal() - repertoryUpdateInVo.getChangeTotal());
                }
            }
            /*修改商品上下架状态*/
            shopRepertoryDto.setIsSale(repertoryUpdateInVo.getIsSale());
            repertoryTotalService.updateShopRepertory(shopRepertoryDto);
        } catch (NullPointerException e) {
            return CommonResult.failed();
        }
        return CommonResult.success(repertoryUpdateInVo.getSkuId());
    }

    @Override
    @ApiOperation("分配库存")
    @PutMapping("/total/allocate")
    public CommonResult allocate(RepertoryTotalAllocateInVo repertoryTotalAllocateInVo) {
        /*检查该商家是否有权管理此商品*/
        if (!check.checkEnterpriseSku(repertoryTotalAllocateInVo.getEnterpriseId(), repertoryTotalAllocateInVo.getSkuId())) {
            return CommonResult.failed("该商家无法管理该商品");
        }
        /*查找商家该sku库存量是否大于分配量*/
        EnterpriseRepertoryDto enterpriseRepertoryDto = repertoryTotalService.findEnterpriseRepertoryBySkuId(repertoryTotalAllocateInVo.getSkuId());
        if (enterpriseRepertoryDto.getRemainTotal() < repertoryTotalAllocateInVo.getAllocateTotal()) {
            return CommonResult.failed("商品库存不足！");
        }
        /*检查该商品是否已经被分配*/
        List<Long> skuIdList = repertoryTotalService.findSkuIdByShopId(repertoryTotalAllocateInVo.getShopId());

        if (skuIdList.contains(repertoryTotalAllocateInVo.getSkuId())) {
            /*已分配，做更新操作*/
            ShopRepertoryDto shopRepertoryDto = repertoryTotalService.findShopRepertoryBySkuId(repertoryTotalAllocateInVo.getSkuId());
            if (repertoryTotalAllocateInVo.getAllocateType() == RepertoryChangeEnum.库存增加.getCode()) {
                /*分配库存*/
                shopRepertoryDto.setDistributeTotal(shopRepertoryDto.getDistributeTotal() + repertoryTotalAllocateInVo.getAllocateTotal());
                shopRepertoryDto.setRemainTotal(shopRepertoryDto.getRemainTotal() + repertoryTotalAllocateInVo.getAllocateTotal());
                enterpriseRepertoryDto.setRemainTotal(enterpriseRepertoryDto.getRemainTotal() - repertoryTotalAllocateInVo.getAllocateTotal());
            } else if (repertoryTotalAllocateInVo.getAllocateType() == RepertoryChangeEnum.库存减少.getCode()) {
                /*回收库存*/
                shopRepertoryDto.setDistributeTotal(shopRepertoryDto.getDistributeTotal() - repertoryTotalAllocateInVo.getAllocateTotal());
                shopRepertoryDto.setRemainTotal(shopRepertoryDto.getRemainTotal() - repertoryTotalAllocateInVo.getAllocateTotal());
                enterpriseRepertoryDto.setRemainTotal(enterpriseRepertoryDto.getRemainTotal() + repertoryTotalAllocateInVo.getAllocateTotal());
            }
            repertoryTotalService.updateEnterpriseRepertory(enterpriseRepertoryDto);
            repertoryTotalService.updateShopRepertory(shopRepertoryDto);
            return CommonResult.success("已为店铺修改库存");
        } else {
            /*未分配，做添加操作*/
            ShopRepertoryDto shopRepertoryDto = ShopRepertoryDto.builder()
                    .skuId(repertoryTotalAllocateInVo.getSkuId())
                    .shopId(repertoryTotalAllocateInVo.getShopId())
                    .distributeTotal(repertoryTotalAllocateInVo.getAllocateTotal())
                    .remainTotal(repertoryTotalAllocateInVo.getAllocateTotal())
                    .build();
            enterpriseRepertoryDto.setRemainTotal(enterpriseRepertoryDto.getRemainTotal() - repertoryTotalAllocateInVo.getAllocateTotal());
            repertoryTotalService.updateEnterpriseRepertory(enterpriseRepertoryDto);
            repertoryTotalService.addShopRepertory(shopRepertoryDto);
            return CommonResult.success("已为店铺分配库存");
        }
    }

    @Override
    @ApiOperation("添加库存流水记录")
    @PutMapping("/record/add")
    public CommonResult<Long> addRecord(RepertoryRecordAddInVo repertoryAddInVo) {
        return null;
    }

    @Override
    @ApiOperation("根据id查找库存流水")
    @PutMapping("/record/findAll")
    public CommonResult<RepertoryRecordFindAllOutVo> findAllRecord(RepertoryQuery repertoryQuery) {
        return null;
    }

    @Override
    @ApiOperation("根据id查找库存流水")
    @PutMapping("/record/findById")
    public CommonResult<RepertoryRecordFindByIdOutVo> findRecordById(Long repertoryId) {
        return null;
    }

}
